// 工具方法 - start
// 比较是否在今天前 
let compareNowDay = data=>{
  var date = data.split('-');
  var year = date[0];
  var month = date[1];
  var day = date[2];
  var d1 = new Date(year + '/' + month + '/' + day);
  var dd = new Date();
  var y = dd.getFullYear();
  var m = dd.getMonth() + 1;
  var d = dd.getDate();
  var d2 = new Date(y + '/' + m + '/' + d);
  var iday = parseInt(d2 - d1) / 1000 / 60 / 60 / 24;
  return iday;
}
// 1.为了获得每个月的日期有多少，我们需要判断 平年闰年[四年一闰，百年不闰，四百年再闰]
const isLeapYear = (year) => {
  return (year % 400 === 0) || (year % 100 !== 0 && year % 4 === 0);
};
// 2.获得每个月的日期有多少，注意 month - [0-11]
const getMonthCount = (year, month,type) => {
  let arr = [
    31, null, 31, 30,
    31, 30, 31, 31,
    30, 31, 30, 31
  ];
  let count = arr[month] || (isLeapYear(year) ? 29 : 28);
  return Array.from(new Array(count), (item, value) => {
    let date = (`${year}-${(month + 1 > 9) ? (month + 1) : ('0' + (month + 1))}-${(value + 1 > 9) ? (value + 1) : ('0' + (value + 1))}`);
    let today = compareNowDay(date);
    return {
     isToday: today === 0 ? 0 : (today > 0 ? -1 : (today < 0?1:'')),
      type,
      date,
      year,
      month: (month + 1 > 9) ? (month + 1) : ('0' + (month + 1)),
      day: (value + 1 > 9) ? (value + 1) : ('0' + (value + 1))
    }
  });
};


// 3.获得某年某月的 1号 是星期几，这里要注意的是 JS 的 API-getDay() 是从 [日-六](0-6)，返回 number
const getWeekday = (year, month) => {
  let date = new Date(year, month, 1);
  return date.getDay();
};
// 4.获得上个月的天数
const getPreMonthCount = (year, month) => {
  if (month === 0) {
    return getMonthCount(year - 1, 11,'pre');
  } else {
    return getMonthCount(year, month - 1, 'pre');
  }
};
// 5.获得下个月的天数
const getNextMonthCount = (year, month) => {
  if (month === 11) {
    return getMonthCount(year + 1, 0,'next');
  } else {
    return getMonthCount(year, month + 1, 'next');
  }
};
export function getCalendar(inDate){
  let date = inDate ? new Date(inDate):new Date(),
  year = date.getFullYear(),
  month = date.getMonth(),
  currentMonth = getMonthCount(year, month,'curent'),
  preMonthCount = getPreMonthCount(year, month),
  nextMonthCount = getNextMonthCount(year, month),
  whereMonday = getWeekday(year, month);
   if (whereMonday === 0) {
      whereMonday = 7
   }
   let preArr = preMonthCount.slice(-1 * (whereMonday-1))
   let nextArr = nextMonthCount.slice(0, 42 - currentMonth.length - (whereMonday - 1));
    if ((preArr.length + nextArr.length) === 42) {
      return {
        year,
        month: month + 1,
        range: [preArr[0].date, currentMonth[currentMonth.length - 1].date],
        list: [...preArr, ...currentMonth]
      }
    }
   if ((currentMonth.length + nextArr.length)===42){
     return {
       year,
       month: month + 1,
       range: [currentMonth[0].date, nextArr[nextArr.length - 1].date],
       list: [...currentMonth, ...nextArr]
     }
   }
   return {
     year,
     month: month + 1,
     range: [preArr[0].date, nextArr[nextArr.length - 1].date],
     list: [...preArr, ...currentMonth, ...nextArr]
   }
}

